﻿using Dapper;
using System.Data.SqlClient;
using yunbo.Model.SM;
using yunbo.Model.SSM;

namespace yunbo.Repositorys.SMRepositorys
{
    public class WMS_CKBRepository
    {
        public string data = "Data Source=121.37.93.71;Initial Catalog=FanXingMES;User ID=sa;Password=geili@2025;";
        public int PostWMS_CK(TJWMS_CKB tjwms_ck)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"insert into wms_ck([ckb_code],[ckb_type],[ck_code],[ckb_fzr],[ckb_jhcksj],[ckb_shzt],[create_by],[create_time])values\r\n" +
                    $"('{tjwms_ck.ckb_code}','{tjwms_ck.ckb_type}','{tjwms_ck.ck_code}',{tjwms_ck.ckb_fzr},'{tjwms_ck.ckb_jhcksj}',{tjwms_ck.ckb_shzt},{tjwms_ck.create_by},'{tjwms_ck.create_time}')";
                int rs = connection.Execute(sql);
                return rs;
            }
        }
        public FYCXWMS_CKB GetCXWMS_CKB(int PageNumber, int PageSize)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"select a.ck_code,b.rkw_code,a.ckb_id,ckb_shzt,a.ckb_code,a.ckb_type,b.wldb_code,b.wldb_dblx,a.ckb_jhcksj,c.ck_name,d.user_uname from wms_ck a\r\n" +
                    $"inner join [dbo].[wms_wldb] b on a.ckb_code=b.ckb_code\r\n" +
                    $"inner join [dbo].[wbd_ck] c on a.ck_code=c.ck_code\r\n" +
                    $"inner join [dbo].[user] d on a.ckb_fzr=d.user_id\r\n" +
                    $"where a.ckb_type='调拨出库' \r\n" +
                    $"ORDER BY  a.[ckb_id] \r\n" +
                    $"OFFSET ({PageNumber}-1)*{PageSize} ROWS\r\n" +
                    $"FETCH NEXT {PageSize} ROWS ONLY;";
                List<CXWMS_CKB> cXWMS_CKBs = connection.Query<CXWMS_CKB>(sql).ToList();
                string sql1 = $"select count(*) from wms_ck a\r\n" +
                    $"inner join [dbo].[wms_wldb] b on a.ckb_code=b.ckb_code\r\n" +
                    $"inner join [dbo].[wbd_ck] c on a.ck_code=c.ck_code\r\n" +
                    $"inner join [dbo].[user] d on a.ckb_fzr=d.user_id\r\n" +
                    $"where a.ckb_type='调拨出库'";
                int zon = connection.Query<int>(sql1).FirstOrDefault();
                FYCXWMS_CKB fYCXWMS_CKB = new FYCXWMS_CKB()
                {
                    cXWMS_CKBs = cXWMS_CKBs,
                    zon = zon,
                };
                return fYCXWMS_CKB;
            }
        }
        public int UpdateWMS_CK(int fzr, int uid, string code, int zt)
        {
            using (SqlConnection connection = new SqlConnection(data))
            {
                string sql = $"update [dbo].[wms_ck] set [ckb_shzt]={zt},ckb_fzr={fzr},update_by={uid},update_time='{DateTime.Now}',ckb_time='{DateTime.Now}' where [ckb_code]='{code}'";
                int rs = connection.Execute(sql);
                return rs;
            }
        }
    }
}
